package systems.jMenuBuilder.elements.items;

import ihm.actions.ActionMenu;

import java.awt.event.ActionEvent;

import javax.swing.ImageIcon;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;

import org.jdom.Element;

import systems.languages.MultiLanguages;

public class MenuItem extends JMenuItem implements Items{

  private static final long serialVersionUID = 1L;

  private String nameEntryKey;
  private String icoPath;
  private String tooltipEntryKey;
  private char acceleratorKey;
  private String acceleratorMask;
  private char mnemonicKey;
  private String actionClass; //RFU

  public MenuItem()
  {
    super();
  }

  public MenuItem(String nameEntryKey, String icoPath, String tooltipEntryKey,
                  char acceleratorKey, String acceleratorMask, char mnemonicKey
  ){}

  public MenuItem(Element item)
  {
    super();
    //Save the generals data relative to the menu.
    this.nameEntryKey =     item.getChildText("name");
    this.actionClass =      item.getChildText("actionClass");
    this.icoPath =          item.getAttributeValue("ico");
    this.tooltipEntryKey =  item.getAttributeValue("tooltip");
    String tmpAcceleratorKey = item.getAttributeValue("accelerator_key");
    if(tmpAcceleratorKey != null)
      this.acceleratorKey = tmpAcceleratorKey.toUpperCase().charAt(0);
    this.acceleratorMask =  item.getAttributeValue("accelerator_mask");
    String tmpMnemonicKey = item.getAttributeValue("mnemonic_key");

    //Set the ToolTips and The Text
    this.update();

    //Set the mnemonic
    if(tmpMnemonicKey != null)
      this.mnemonicKey =    tmpMnemonicKey.toUpperCase().charAt(0);

    //Set the Icon
    if(this.icoPath != null)
      super.setIcon(new ImageIcon(this.icoPath));

    //Set the Accelerator
    if(this.acceleratorKey != 0)
    { 
      if(this.acceleratorMask.equals("ctrl"))
        super.setAccelerator(
            KeyStroke.getKeyStroke(
                this.acceleratorKey, ActionEvent.CTRL_MASK));
      else if(this.acceleratorMask.equals("alt"))
        super.setAccelerator(
            KeyStroke.getKeyStroke(
                this.acceleratorKey, ActionEvent.ALT_MASK));
      else if(this.acceleratorMask.equals("shift"))
        super.setAccelerator(
            KeyStroke.getKeyStroke(
                this.acceleratorKey, ActionEvent.SHIFT_MASK));
    }
    super.setMnemonic(this.mnemonicKey);

  }

  public String toString()
  {
    return super.getName();
  }

  public void update()
  {
    setText(MultiLanguages.getProperties(this.nameEntryKey));
    if(this.tooltipEntryKey != null)
      super.setToolTipText(MultiLanguages.getProperties(this.tooltipEntryKey));
  }

  /**
   * RFU
   */
  private void setAction()
  {
    /*
      if(!this.actionClass.trim().isEmpty())

    {
      Class actionClass = Class.forName(this.actionClass.trim());
      ActionMenu actionMenu = (ActionMenu) actionClass.newInstance();
      actionMenu.init();
      this.addActionListener(actionMenu);
    }
     */
  }
}
